package com.thread.longadder;

import java.util.concurrent.atomic.LongAccumulator;
import java.util.concurrent.atomic.LongAdder;

/**
 * @author wangyingjie
 * @date
 * @remark
 */
public class LongAddrDemo implements Runnable{

    private LongAdder longAdder;

    private static LongAccumulator longAccumulator;

    @Override
    public void run() {
//        longAdder.increment();
        longAccumulator.accumulate(2);//起始值
    }

    public LongAddrDemo(LongAdder longAdder) {
        this.longAdder = longAdder;
    }

    public LongAddrDemo(LongAccumulator longAccumulator) {
        this.longAccumulator = longAccumulator;
    }

    public static void main(String[] args) throws InterruptedException {

        LongAdder longAdder = new LongAdder();
        for (int i = 0; i < 3; i++) {
//            new Thread(new LongAddrDemo(longAdder)).start();
            new Thread(new LongAddrDemo(new LongAccumulator(Long::max,Long.MIN_VALUE))).start();
        }
        Thread.sleep(2000);
//        System.out.println(longAdder.sum());
        System.out.println(longAccumulator.get());
    }
}
